﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;
using System.Windows.Media.Effects;

namespace SilverlightSdms.UserControls.Scheduler
{
    public partial class UserControlCalendar : UserControl
    {
        Label[] m_lblsDaysOfTheWeek = new Label[7];
        UserControlDay[,] m_DaysOfMonth = new UserControlDay[6, 7];
        UserControlDay m_FocusDay = null;
        int m_CurrentMonth = DateTime.Now.Month;
        int m_CurrentYear = DateTime.Now.Year;
        public int UserId { get; set; }

        public UserControlCalendar()
        {
            InitializeComponent();
            InitializeCalendar();
            string monthName = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[m_CurrentMonth - 1];
            txtBlockCurrentMonth.Text = monthName + " ," + m_CurrentYear;
        }

        private void InitializeCalendar()
        {
            string[] days = CultureInfo.CurrentCulture.DateTimeFormat.DayNames;
            
            for (int i = 0; i < days.Length; i++)
            {
                m_lblsDaysOfTheWeek[i] = new Label();
                m_lblsDaysOfTheWeek[i].Effect = new DropShadowEffect();
                m_lblsDaysOfTheWeek[i].HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                m_lblsDaysOfTheWeek[i].Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xE5, 0xE5, 0xE5));  // alpha, red, green, blue
                m_lblsDaysOfTheWeek[i].Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xA8, 0xFF));
                m_lblsDaysOfTheWeek[i].Content = days[i];
                m_lblsDaysOfTheWeek[i].Margin = new Thickness(0, 0, 0, 4);
                LayoutRoot.Children.Add(m_lblsDaysOfTheWeek[i]);
                Grid.SetColumn(m_lblsDaysOfTheWeek[i], i);
                Grid.SetRow(m_lblsDaysOfTheWeek[i], 0);
            }

            for (int i = 0; i < m_DaysOfMonth.GetLength(0); i++)
            {
                for (int j = 0; j < m_DaysOfMonth.GetLength(1); j++)
                {
                    m_DaysOfMonth[i, j] = new UserControlDay();
                    LayoutRoot.Children.Add(m_DaysOfMonth[i,j]);
                    Grid.SetColumn(m_DaysOfMonth[i, j], j);
                    Grid.SetRow(m_DaysOfMonth[i, j], i+1);
                }
            }
            InitDates();
        }

        public void InitDates(int UserId)
        {
            this.UserId = UserId;
            InitDates();
        }

        private void InitDates()
        {

            for (int i = 0; i < m_DaysOfMonth.GetLength(0); i++)
            {
                for (int j = 0; j < m_DaysOfMonth.GetLength(1); j++)
                {
                    m_DaysOfMonth[i, j].ResetDate();
                    try
                    {
                        m_DaysOfMonth[i, j].MouseEnter -= UserControlCalendar_MouseEnter;
                        m_DaysOfMonth[i, j].MouseLeave -= UserControlCalendar_MouseLeave;
                        m_DaysOfMonth[i, j].MouseLeftButtonUp -= UserControlCalendar_MouseLeftButtonUp;
                        m_DaysOfMonth[i, j].MouseLeftButtonUp -= m_DaysOfMonth[i, j].UserControl_MouseLeftButtonUp;
                        m_DaysOfMonth[i, j].RemoveHandler(UserControlDay.MouseLeftButtonUpEvent, new MouseButtonEventHandler(UserControlCalendar_MouseLeftButtonUp));
                        m_DaysOfMonth[i, j].RemoveHandler(UserControlDay.MouseLeftButtonUpEvent, new MouseButtonEventHandler(m_DaysOfMonth[i, j].UserControl_MouseLeftButtonUp));
                    }
                    catch { }
                }
            }

            var firstDayInMonth = new DateTime(m_CurrentYear, m_CurrentMonth, 1);
            int dayInWeek = (int)firstDayInMonth.DayOfWeek - 1;
            int weekInMonth = 0;

            for (int i = 0; i < DateTime.DaysInMonth(m_CurrentYear, m_CurrentMonth); i++)
            {
                dayInWeek++;
                dayInWeek %= 7;
                if (dayInWeek == 0 & i != 0)
                {
                    weekInMonth++;
                }
                m_DaysOfMonth[weekInMonth, dayInWeek].UserId = UserId;
                m_DaysOfMonth[weekInMonth, dayInWeek].SetDate(i + 1, m_CurrentMonth, m_CurrentYear);
                m_DaysOfMonth[weekInMonth, dayInWeek].MouseEnter += UserControlCalendar_MouseEnter; //new MouseEventHandler(UserControlCalendar_MouseEnter);
                m_DaysOfMonth[weekInMonth, dayInWeek].MouseLeave += UserControlCalendar_MouseLeave; //new MouseEventHandler(UserControlCalendar_MouseLeave);
                m_DaysOfMonth[weekInMonth, dayInWeek].MouseLeftButtonUp += UserControlCalendar_MouseLeftButtonUp;//new MouseButtonEventHandler(UserControlCalendar_MouseLeftButtonUp);
                m_DaysOfMonth[weekInMonth, dayInWeek].MouseLeftButtonUp += m_DaysOfMonth[weekInMonth, dayInWeek].UserControl_MouseLeftButtonUp;//new MouseButtonEventHandler(m_DaysOfMonth[weekInMonth, dayInWeek].UserControl_MouseLeftButtonUp); 
                m_DaysOfMonth[weekInMonth, dayInWeek].IsHitTestVisible = true;
                m_DaysOfMonth[weekInMonth, dayInWeek].AddHandler(UserControlDay.MouseLeftButtonUpEvent, new MouseButtonEventHandler(UserControlCalendar_MouseLeftButtonUp), true);
                m_DaysOfMonth[weekInMonth, dayInWeek].AddHandler(UserControlDay.MouseLeftButtonUpEvent, new MouseButtonEventHandler(m_DaysOfMonth[weekInMonth, dayInWeek].UserControl_MouseLeftButtonUp), true);
            }
        }

        void UserControlCalendar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //remove color from prev selected day
            if (m_FocusDay != null)
            {
                m_FocusDay.LayoutRoot.Background = new SolidColorBrush();
            }
            //set m_FocusDay to current selected day and color it
            m_FocusDay = sender as UserControlDay;
            m_FocusDay.LayoutRoot.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xA8, 0xFF));
        }

        void UserControlCalendar_MouseLeave(object sender, MouseEventArgs e)
        {
            if (m_FocusDay != sender as UserControlDay)
            {
                (sender as UserControlDay).LayoutRoot.Background = new SolidColorBrush();
            }
        }

        void UserControlCalendar_MouseEnter(object sender, MouseEventArgs e)
        {
            if (m_FocusDay != sender as UserControlDay)
            {
                (sender as UserControlDay).LayoutRoot.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xD5, 0xD5, 0xD5));
            }
        }

        private void btnRightArrow_Click(object sender, RoutedEventArgs e)
        {
            m_CurrentMonth = m_CurrentMonth + 1;
            if (m_CurrentMonth > 12)
            {
                m_CurrentMonth = 1;
                m_CurrentYear++;
            }
            string monthName = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[m_CurrentMonth - 1];
            txtBlockCurrentMonth.Text = monthName + " ," + m_CurrentYear;
            InitDates();
        }

        private void btnLeftArrow_Click(object sender, RoutedEventArgs e)
        {
            m_CurrentMonth = m_CurrentMonth - 1;
            if (m_CurrentMonth < 1)
            {
                m_CurrentMonth = 12;
                m_CurrentYear--;
            }
            string monthName = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[m_CurrentMonth - 1];
            txtBlockCurrentMonth.Text = monthName + " ," + m_CurrentYear;
            InitDates();
        }

    }
}
